package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spannable;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.chatuidemo.DemoApplication;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.ChatCard;

import com.joplus.housekeeper.utils.L;

import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;


/**
 * 普通卡片订单
 */
public class EaseChatRowText_gif extends EaseChatRow {

    private TextView contentView;

    private LinearLayout layoutCard, layoutCardList;
    private TextView tvCardSubject, tvCardPay;

    private LinearLayout layoutTip;
    private LinearLayout layoutFeed;
    private ImageView ivUnS;
    private ImageView ivS;
    private TextView tvUnS;
    private TextView tvS;
    private TextView tvF;
    private LinearLayout llUnS;
    private LinearLayout lls;

    private String rewarId;
    private String isClick;

    private ImageView gif;


    public EaseChatRowText_gif(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);

    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_message_gif : R.layout.ease_row_sent_message_gif, this);


    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);

        gif = (ImageView) findViewById(R.id.gif);


    }

    @Override
    public void onSetUpView() {
        /*if (message.getBody()!=null){*/
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        //      contentView.setText(span, BufferType.SPANNABLE);


        String msg = txtBody.getMessage();
//        if(msg.contains("&quot;")){
//            msg.replace("&quot;","\"\"");
//        }
        try {
            new JSONObject(msg);
            Gson gson = new Gson();
            ChatCard chatCard = gson.fromJson(txtBody.getMessage(), ChatCard.class);
            //   bubbleLayout.setVisibility(View.GONE);
            //   layoutCardList.removeAllViews();
            contentView.setVisibility(View.GONE);
            gif.setVisibility(VISIBLE);
            //  setGif(chatCard.getMessage() + ".gif");
            Glide.with(context).load("file:///android_asset/" + chatCard.getMessage() + ".gif").asGif().diskCacheStrategy(DiskCacheStrategy.NONE).into(gif);


            chatCard.getMessage();
            Log.e(TAG, "onSetUpView:-----------------------show " + "file:///android_asset/" + chatCard.getMessage() + ".gif");

        } catch (Exception e) {
            Log.e(TAG, "onSetUpView:---------------------not--show " + e.toString());
            gif.setVisibility(GONE);
            bubbleLayout.setVisibility(View.VISIBLE);
            contentView.setVisibility(View.VISIBLE);
        }
        try {
            handleTextMessage();//不知道什么鬼，里面的processbar竟然出现空指针！
        } catch (Exception e) {

        }


    }

    public void setGif(String fileName) {
        InputStream bitmap = null;
        try {
            bitmap = context.getAssets().open(fileName); //参数，取assets文件夹下资源文件的路劲，不包括"assets/"
            Bitmap bit = BitmapFactory.decodeStream(bitmap);
            gif.setImageBitmap(bit);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void orderStatus(final ChatCard chatCard) {
    }

    private void setOrderStatus(String info, final ChatCard chatCard) {
        String txtStatus = "";

        JSONObject jsonObject = null;
        L.e("ChatCard" + " " + chatCard.toString());



    }


    protected void handleTextMessage() {
      /*  if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
                    //               sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }*/
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        L.e("点击了聊天文字");
    }

    private boolean isMyCard(String cardaccoundId) {
        if (cardaccoundId != null) {
            return cardaccoundId.equals(DemoApplication.getInstance().getUser().getData().getAccountId());

        }
        return true;
    }

}
